﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace DataLayer
{
    public class WindowsHelper : SystemFileHelper
    {
        public static XmlDocument xmlDocument;
        public static string DestinationFile;
        public static List<SystemFile> ListWindow=new List<SystemFile>();
        public static void CreateWorkSpaceFile(string folderDir)
        {
            xmlDocument = new XmlDocument();
            XmlNode winList = xmlDocument.CreateNode(XmlNodeType.Element, "WindowList", "");
            xmlDocument.AppendChild(winList);
            DestinationFile = folderDir + "\\" + "Workspace.xml";
            xmlDocument.Save(DestinationFile);
        }
        public override List<SystemFile> LoadAll()
        {
            ListWindow.Clear();
            if (System.IO.File.Exists(DestinationFile))
            {
                xmlDocument = new XmlDocument();
                XmlTextReader reader = new XmlTextReader(DestinationFile);
                reader.Read();
                // load reader
                xmlDocument.Load(reader);
                for (int i = 0; i < xmlDocument.ChildNodes[0].ChildNodes.Count; i++)
                {
                    XmlNode currentNode = xmlDocument.ChildNodes[0].ChildNodes[i];
                   
                        MyWindow window = new MyWindow();
                        window.ContentId = int.Parse(currentNode.SelectSingleNode("ContentId").InnerText);
                        if (currentNode.SelectSingleNode("status").InnerText.Equals("False"))
                            window.Status = false;
                        else
                            window.Status = true;
                        if (currentNode.SelectSingleNode("IsSelected").InnerText.Equals("False"))
                            window.IsSelected = false;
                        else
                            window.IsSelected = true;
                        ListWindow.Add(window);}
                return ListWindow;
            }
            return null;
        }
           static public MyWindow GetWindowsByFileName(string fileName)
        {
            foreach (MyWindow win in ListWindow)
            {
                if (ResourcesHelper.GetInstance().IDToPath(win.ContentId ).Equals(fileName))
                    return win;
            }
            return null;
        }
        public void RemoveWindow(MyWindow myWin)
        {
            if (myWin != null)
            {
                XmlNode deleteNode = xmlDocument.FirstChild.SelectSingleNode("./Window[ContentId='" + myWin.ContentId + "']");
                if (deleteNode != null)
                {
                    xmlDocument.FirstChild.RemoveChild(deleteNode);
                    ListWindow.Remove(myWin);
                }
            }
        }
        public void AddWindows(MyWindow myWin)
        {
            XmlNode winNode = xmlDocument.CreateNode(XmlNodeType.Element,"Window","");
            XmlNode statusNode = xmlDocument.CreateNode(XmlNodeType.Element, "status", "");
            statusNode.InnerText = myWin.Status.ToString();
            XmlNode isSelectedNode = xmlDocument.CreateNode(XmlNodeType.Element, "IsSelected", "");
            statusNode.InnerText = myWin.IsSelected.ToString();
            XmlNode ContainIDNode = xmlDocument.CreateNode(XmlNodeType.Element, "ContentId", "");
            ContainIDNode.InnerText = myWin.ContentId.ToString();
            winNode.AppendChild(statusNode);
            winNode.AppendChild(ContainIDNode);
            winNode.AppendChild(isSelectedNode);
            xmlDocument.FirstChild.AppendChild(winNode);
            xmlDocument.Save(DestinationFile);
            ListWindow.Add(myWin);
        }
        static WindowsHelper instance=null;
        static public WindowsHelper GetInstance()
        {
            if (instance == null)
                instance = new WindowsHelper();
            return instance;
        }
        public void Reset()
        {
            xmlDocument.FirstChild.RemoveAll();
            
        }
        public void save()
        {
            xmlDocument.Save(DestinationFile);
        }
        public override void Update()
        {
            base.Update();
        }
    }
}
